<template>
  <div class="announcement-detail">
    <div class="form-wrap">
      <el-form label-position="top" label-width="80px" :model="formData">
        <el-form-item l:label="$t('configLazyLoad.process.nodeName')">
          <el-input v-model.trim="formData.DisplayName"></el-input>
        </el-form-item>

        <div class="edit-ivr-wrap" style="border: 1px solid #E7E7EB;margin-bottom: 20px;">
          <el-row style="padding: 6px 12px 4px 12px;background-color: #F4F5F9;">
            <el-col :span="6">变量</el-col>
            <el-col :span="4">条件</el-col>
            <el-col :span="6">值</el-col>
            <el-col :span="6">逻辑关系</el-col>
            <el-col :span="2"><i @click="addIvrItem" class="el-icon-circle-plus" style="color: #409EFF;font-size: 18px;cursor: pointer;"></i></el-col>
          </el-row>
          <div>
            <el-row v-for="(item, index) in formData.Routes" style="padding: 6px 12px 14px 12px;border-bottom: 1px solid #E7E7EB;">
              <el-col :span="6">
                <el-select v-model="item.VarName" placeholder="请选择" size="mini" style="width: 100%;">
                  <el-option
                    v-for="one in vars.vars"
                    :label="one.VarName"
                    :value="one.VarName">
                  </el-option>
                </el-select>
              </el-col>
              <el-col :span="4">
                <el-select v-model="item.Operation" placeholder="请选择" size="mini" style="width: 100%;">
                  <el-option label="前缀匹配" value="0"></el-option>
                  <el-option label="后缀匹配" value="1"></el-option>
                  <el-option label="等于" value="2"></el-option>
                </el-select>
              </el-col>
              <el-col :span="6"><el-input v-model="item.Value"></el-input></el-col>
              <el-col :span="6">
                <el-select v-model="item.Logic" placeholder="请选择" size="mini" style="width: 100%;">
                  <el-option label="并且" value="and"></el-option>
                  <el-option label="或者" value="or"></el-option>
                </el-select>
              </el-col>
              <el-col :span="2"><i @click="delIvrItem(index)" class="el-icon-remove" style="color: #FF6B6B;font-size: 18px;margin-top: 4px;cursor: pointer;"></i></el-col>
            </el-row>
          </div>
        </div>

        <el-form-item label="满足条件转向：">
          <node-select-item ref="nodeSelect" :curPbx="curPbx" :ToPeerType="form.ToPeerType" :ToPeer="form.ToPeer"></node-select-item>
        </el-form-item>
        <el-form-item label="不满足条件转向：">
          <node-select-item ref="nodeSelectUnmatched" :curPbx="curPbx" :ToPeerType="form.ToPeerTypeUnmatched" :ToPeer="form.ToPeerUnmatched"></node-select-item>
        </el-form-item>

      </el-form>
    </div>
    <div class="footer">
      <el-button class='slide-box__button' @click='cancelClick'>
        取消
      </el-button>
      <el-button class='slide-box__button' type="primary" @click='saveClick'>
        保存
      </el-button>
    </div>
  </div>
</template>
<script>
  import {deepClone} from '@/utils/m7Utils'
  import nodeSelectItem from '../../base/nodeSelectItem.vue'
  export default {
    name: 'mainIvrRouteNodeLink',
    data () {
      return {
        formData: {}
      }
    },
    props: {
      form: {
        type: Object,
        default: function () {
          return {}
        }
      }
    },
    components: {
      nodeSelectItem
    },
    computed: {
      Account () {
        return this.$store.state.session.account.account
      },
      curPbx () {
        return this.$store.state.config.callCenter.currentPBX
      },
      vars () {
        return this.$store.state.session.ivrMap[this.curPbx].data
      }
    },
    methods: {
      init () {
        this.formData = deepClone(this.form)
      },
      cancelClick () {
        this.$emit('cancelClick')
      },
      saveClick () {
        if (!this.formData.DisplayName) {
          this.$message({
            type: 'error',
            message: '节点名称不能为空!'
          })
          return
        }
        if (this.formData.Routes.length > 20) {
          this.$message({
            type: 'error',
            message: '变量路由节点最多添加20个条件!'
          })
          return
        }
        let flag = false
        this.formData.Routes.forEach(item => {
          if (!item.VarName || !item.Value) {
            flag = true
          }
        })
        if (flag) {
          this.$message({
            type: 'error',
            message: '变量和值不能为空'
          })
          return
        }
        this.formData.ToPeerType = this.$refs.nodeSelect.ToPeerTypeThis
        this.formData.ToPeer = this.$refs.nodeSelect.ToPeerThis
        this.formData.ToPeerTypeUnmatched = this.$refs.nodeSelectUnmatched.ToPeerTypeThis
        this.formData.ToPeerUnmatched = this.$refs.nodeSelectUnmatched.ToPeerThis
        let obj = deepClone(this.formData)
        obj.PBX = this.curPbx
        obj.Account = this.Account
        this.$store.dispatch('updateRoute', obj).then(res => {
          this.$message({
            type: 'success',
            message: '保存成功!'
          })
          this.updateVuexPbx()
        })
      },
      updateVuexPbx () {
        this.$store.dispatch('queryRoute', {Account: this.Account, PBX: this.curPbx}).then(resp => {
          if (resp && resp.success) {
            this.$store.commit('SET_IVRMAP_DETAIL', {PBX: this.curPbx, typeName: 'ivr_node_route', data: resp.list || [], flag: 'data', node: this})
          }
          this.$emit('saveClick')
        })
      },
      addIvrItem () {
        let obj = {
          VarName: '',
          Operation: '0',
          Value: '',
          Logic: 'and'
        }
        if (this.formData.Routes) {
          this.formData.Routes.push(obj)
        } else {
          let arr = []
          arr.push(obj)
          this.$set(this.formData, 'Routes', arr)
        }
      },
      delIvrItem (index) {
        this.formData.Routes.splice(index, 1)
      }
    },
    watch: {
      form: {
        deep: true,
        handler (nv, ov) {
          this.init()
          this.$refs.nodeSelect.init()
          this.$refs.nodeSelectUnmatched.init()
        }
      }
    },
    beforeMount () {
      this.init()
    }
  }
</script>

<style lang="stylus" scoped>
  .announcement-detail
    position relative
    width 100%
    height 100%
  .form-wrap
    padding 20px
    height calc(100% - 100px)
    overflow-y scroll
    .el-col
      padding-right 12px
  .edit-ivr-wrap
    /*max-height calc(100vh - 350px)*/
    /*overflow-y scroll*/
    background-color #fff
  .footer
    position absolute
    bottom 0
    left 0
    width 100%
    background rgba(255,255,255,1)
    box-shadow 0px -2px 4px 0px rgba(5,49,38,0.05)
    border-top 1px solid rgba(232,232,232,1)
    display flex
    justify-content center
    padding 13px 0
    box-sizing border-box
    .slide-box__button
      padding 8px 20px 8px
      box-sizing border-box
</style>
